#ifndef SEATRESMANAGER_HPP
#define SEATRESMANAGER_HPP

#include "dialoglogin.h"
#include "QSqlDatabase"
#include <QMessageBox>
#include <QSqlQuery>
#include <QtSql>
#include <QDebug>
#include <QSqlError>
#include <QSqlTableModel>
#include "adminpage.h"
#include "userpage.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <commontype.h>

class SeatResManager: public QObject
{
Q_OBJECT
public:
    SeatResManager();
    void DeleteTableRow(QString tableName, SqlItemData itemData);
    void EditTableItem(QString tableName, SqlItemData targetRow, SqlItemData itemData);
    std::vector<SqlTableData> ReadDataFromTable(QString tableName);
    void WriteDataToTable(QString tableName, QStringList headerList, std::vector<SqlTableData> tableData);
    void PushBackDataToTable(QString tableName, QStringList headerList, std::vector<SqlTableData> tableData);
    void LoadUserList(std::vector<SqlTableData> tableData);
    void LoadSeatList(std::vector<SqlTableData> tableData);
    void RecvNewTcpMsg(QString addr, QString msg);

signals:
    void PostUserTable(std::map<QString, StuData>);
    void PostUserState(StuData);
    void PostSeatTable(std::map<QString, SeatInfo>);

private slots:
    void onPostLoginData(StuData studata);
    void onPostRegisterData(StuData studata);
    void onPostDelUser(QString stuid);
    void onPostDelSeat(QString seatid);
    void onPostEditUserPasswd(StuData studata);
    void onPostNewTcpClient();
    void onPostEditRes(SeatInfo seatInfo);
    void onTimerTimeOut();
    void UnBindSeat(QString stuid);
    void onPostSendCode(QString seatAddr, QString code);

private:
    QSqlDatabase db;
    DialogLogin *dialogLogin_;
    std::map<QString, StuData> stuData_;
    std::map<QString, SeatInfo> seatInfo_;
    AdminPage *adminPage_;
    UserPage *userPage_;
    //server用于监听端口，获取新的tcp连接的描述符
    QTcpServer *server_;
    //存储已连接的socket对象
    std::map<QString, QTcpSocket*> clientMap_;
    QTimer *timer;
};

#endif // SEATRESMANAGER_H
